package com.springboot.cloud.demos.feign.rest;

import com.springboot.cloud.common.core.constant.Constant;
import com.springboot.cloud.common.core.entity.vo.Result;
import com.springboot.cloud.common.core.exception.UserErrorType;
import com.springboot.cloud.demos.entity.enums.PayType;
import com.springboot.cloud.demos.entity.enums.ProfitRecordStatus;
import com.springboot.cloud.demos.entity.enums.ProfitRecordType;
import com.springboot.cloud.demos.entity.form.ProfitRecordForm;
import com.springboot.cloud.demos.entity.form.WalletForm;
import com.springboot.cloud.demos.entity.po.Dictionary;
import com.springboot.cloud.demos.entity.po.ProfitRecord;
import com.springboot.cloud.demos.entity.po.User;
import com.springboot.cloud.demos.entity.po.Wallet;
import com.springboot.cloud.demos.feign.service.DictionaryService;
import com.springboot.cloud.demos.feign.service.ProfitRecordService;
import com.springboot.cloud.demos.feign.service.UnitService;
import com.springboot.cloud.demos.feign.service.WalletService;
import lombok.extern.log4j.Log4j;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author qjmove
 */
@RestController
@Slf4j
public class WalletController extends BaseController {

    @Autowired
    private WalletService walletService;
    @Autowired
    private DictionaryService dictionaryService;
    @Autowired
    private ProfitRecordService profitRecordService;

    @RequestMapping(value = "/wallet", method = RequestMethod.GET)
    public Result query(HttpServletRequest request) {
        User user = getUser(request);
        Result result = walletService.query(user.getId());
        return result;
    }

    /**
     * 支付预扣费
     */
    @RequestMapping(value = "/wallet/prepay", method = RequestMethod.POST)
    public Result pay(HttpServletRequest request, @RequestParam("type") String type) {
        User user = getUser(request);
        Result<Wallet> result = walletService.query(user.getId());
        Result<List<Dictionary>> d = dictionaryService.query(type);
        if (result.isSuccess() && d.isSuccess()) {
            Wallet wallet = result.getData();
            if (wallet == null) {
                return Result.fail(UserErrorType.USER_WALLET_INSUFFICIENT);
            }
            List<Dictionary> list = d.getData();
            if (!list.isEmpty()) {
                Dictionary dictionary = list.get(0);
                int amount = Integer.parseInt(dictionary.getKeyValue());
                if (amount > wallet.getAmount()) {
                    return Result.fail(UserErrorType.USER_WALLET_INSUFFICIENT);
                } else {

                    ProfitRecordForm profitRecordForm = new ProfitRecordForm();
                    profitRecordForm.setType(ProfitRecordType.outcome.getCode());
                    profitRecordForm.setUserId(user.getId());
                    profitRecordForm.setAmount(amount);
                    profitRecordForm.setStatus(ProfitRecordStatus.prepay.getCode());
                    profitRecordForm.setRemark("联系女神支出");
                    log.info("profitRecordForm:{}", profitRecordForm);
                    Result result2 = profitRecordService.save(profitRecordForm);
                    return result2;
                }
            }
        }
        return Result.fail();
    }

    @RequestMapping(value = "/wallet/pay", method = RequestMethod.POST)
    public Result refund(HttpServletRequest request, @RequestParam("id") String id) {
        User user = getUser(request);
        Result<Wallet> result = walletService.query(user.getId());
        Result<ProfitRecord> profitRecordResult = profitRecordService.get(id);
        if (result.isSuccess() && profitRecordResult.isSuccess()) {
            Wallet wallet = result.getData();
            if (wallet == null) {
                return Result.fail(UserErrorType.USER_WALLET_INSUFFICIENT);
            }
            ProfitRecord profitRecord = profitRecordResult.getData();
            if (profitRecord.getStatus().equals(ProfitRecordStatus.pay.getCode())) {
                return Result.fail(UserErrorType.USER_WALLET_FALLBACK);
            }
            WalletForm walletForm = new WalletForm();
            walletForm.setId(wallet.getId());
            walletForm.setAmount(wallet.getAmount() -profitRecord.getAmount());
            walletService.update(walletForm);
            ProfitRecordForm profitRecordForm = new ProfitRecordForm();
            profitRecordForm.setId(id);
            profitRecordForm.setStatus(ProfitRecordStatus.pay.getCode());
            Result result2 = profitRecordService.update(profitRecordForm);
            return result2;
        }
        return result;
    }
}
